package com.zhku.meeting.service;

import com.zhku.meeting.entities.Msg;
import com.zhku.meeting.entities.Staff;
import com.zhku.meeting.mapper.StaffMapper;
import com.zhku.meeting.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@Service
public class LoginService {
//    @Autowired
//    private DepartmentMapper departmentMapper;
    @Autowired
    private StaffMapper staffMapper;

    public Msg login(Map<String , Object> para, HttpServletRequest request){

        String email = (String)para.get("username");
        String pwd = (String)para.get("password");
        Staff loginStaff=new Staff();
        loginStaff.setEmail(email);
        loginStaff.setPassword(pwd);
        Staff s =staffMapper.login(loginStaff);
        if (s!=null){
            String token = TokenUtil.sign(s);
            Map<String,Object> map = new HashMap<>();
            map.put("token",token);
            return Msg.success().add(map);
        }

        int staffId = staffMapper.selectIdByEmail(email);
        request.getSession().setAttribute("staffId",staffId);
        return Msg.fault();
//        String name = (String)para.get("username");
//        String pwd = (String)para.get("password");
//        Department department=new Department();
//        department.setDeptNo(name);
//        department.setDeptPassword(pwd);
//        Department b = departmentMapper.loginMessage(department);
//        if (b!=null){
//            String token = TokenUtil.sign(b);
//            Map<String,Object> map = new HashMap<>();
//            map.put("token",token);
//            return Msg.success().add(map);
//        }
//        return Msg.fault();
    }
}
